package cn.water.rocketmq.common;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.junit.Test;

import java.nio.charset.StandardCharsets;

public class CommonProducer {
    @Test
    public void sendProducer() throws Exception {
        //建立发送者
        DefaultMQProducer producer = new DefaultMQProducer();
        //注册到nameserver
        producer.setNamesrvAddr("localhost:9876");
        //生产者分组
        producer.setProducerGroup("common-producer-group");
        producer.start();

        //编辑消息
        String content = "你是一个大沙雕！";
        Message message = new Message();
        message.setTopic("common-topic");
        byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
        message.setBody(bytes);
        message.setKeys("common-2024-7-29");
        SendResult send = producer.send(message, 2000);

        System.out.println("消息发送状态:"+send.getSendStatus());
        System.out.println("消息发送队列:"+send.getMessageQueue());
        System.out.println("消息id:"+send.getMsgId());

        //关闭消息
        producer.shutdown();
    }
}
